package com.g8.pethospitallearnbackend.controller;

import com.g8.pethospitallearnbackend.entity.Disease;
import com.g8.pethospitallearnbackend.exception.CustomException;
import com.g8.pethospitallearnbackend.service.DiseaseService;
import com.g8.pethospitallearnbackend.service.TypeService;
import com.g8.pethospitallearnbackend.utils.responseUtil.ResponseStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/disease")
public class DiseaseController {
    @Autowired
    private DiseaseService diseaseService;
    @Autowired
    private TypeService typeService;

    /**
     * 返回二级菜单的内容，typeName--diseaseName
     * @param typeName
     * @return
     */
    @GetMapping("/title/{typeName}")
    public List<String> getDiseaseNamesByTypeName(@PathVariable String typeName){
        if(typeName==null){
            throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"typeName不能为空");
        }
        if(typeName.length()>100){
            throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"typeName长度超出限制");
        }
        String typeId=typeService.getTypeIdByName(typeName);
        if(typeId==null){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"该病种名称不存在");
        }
        List<Disease> diseases=diseaseService.getDiseasesByTypeId(typeId);
        if(diseases==null || diseases.isEmpty()||null==diseases.get(0)){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"该病种下面暂无具体病名数据");
        }
        List<String> diseaseNames=new ArrayList<>();
        for(Disease disease:diseases){
            diseaseNames.add(disease.getDiseaseName());
        }
        return diseaseNames;
    }

    @GetMapping("/{diseaseId}")
    public Disease getADiseaseById(@PathVariable String diseaseId){
        return diseaseService.getADiseaseById(diseaseId);
    }

    @GetMapping("")
    public List<Disease> getAllIdAndName(){
        return diseaseService.getAllIdAndName();
    }
}
